//knex操作数据库
const {mysql} = require('../../qcloud')

module.exports = async(ctx) => {
	//goodsKind里分页加载数据
	const page = ctx.query.page
	//轮播图数据
	try{
		// 图片轮播
		const banner = await mysql("nideshop_ad").where({ad_position_id: 1}).select()
		//头部卡片
		const headArr = await mysql('nideshop_headlist').limit(3).select()
		//新品首发
		const newgoodsArr = await mysql('nideshop_goods').where({is_new:1}).limit(6).select()
		// 人气精选
	  	const hotgoodsArr = await mysql('nideshop_goods')
	  						.column('id', 'name', 'list_pic_url', 'retail_price', 'goods_brief')//取出这几个字段
	  						.where({is_hot: 1})
	  						.limit(11)
	  						.select()

		//取出全部新品首发
		const newgoodsList = await mysql('nideshop_goods').where({is_new:1}).limit(9).offset(Number(page)*9).select()
		const newgoodsInfo = await mysql('nideshop_headlist').where({id:4}).select()
		//取出全部人气精选商品
		const hotgoodsList = await mysql('nideshop_goods')
	  						.column('id', 'name', 'list_pic_url', 'retail_price', 'goods_brief')//取出这几个字段
	  						.where({is_hot: 1})
	  						.limit(9).offset(Number(page)*9)
	  						.select()
	  	const hotgoodsInfo = await mysql('nideshop_headlist').where({id:5}).select()

		//9.9专区信息
		const cheapList = await mysql('nideshop_goods').where({retail_price:9.9}).limit(9).offset(Number(page)*9).select()
		const cheapInfo = await mysql('nideshop_headlist').where({id:2}).select()

		//将数据返回前端
	    ctx.state.data = {
	    	banner,newgoodsArr,hotgoodsArr,newgoodsList,cheapList,cheapInfo,hotgoodsInfo,newgoodsInfo,
	    	hotgoodsList,headArr
	    }
	}catch(e){
		ctx.state = {
			code:-1,
			data:{
				msg:"获取失败"+e.sqlMessage
			}
		}
	}
}